<?php

require_once( 'flexical/admin/controller.php' );
require_once( 'flexical/admin/query-request.php' );
require_once( 'flexical/admin/query-response.php' );

class Flexical_Admin_Query_Controller extends Flexical_Admin_Controller
{
	public function respond( $request )
	{
		$response = null;

		if ( $request instanceof Flexical_Admin_Show_Queries_Request )
		{
			$response = $this->show_queries();
		}

		else if ( $request instanceof Flexical_Admin_Create_Query_Request )
		{
			$response = $this->create_query( $request->query );
		}

		else if ( $request instanceof Flexical_Admin_Update_Query_Request )
		{
			if ( $request->cancel_selected )
			{
				$response = $this->show_queries();
				$response->canceled = true;
			}

			else
			{
				$response = $this->update_query( $request->query );
			}
		}

		else if ( $request instanceof Flexical_Admin_Delete_Query_Request )
		{
			if ( $request->cancel_selected )
			{
				$response = $this->show_queries();
				$response->canceled = true;
			}

			else
			{
				$response = $this->delete_query( $request->query );
			}
		}

		else if ( $request instanceof Flexical_Admin_Edit_Query_Request )
		{
			$response = $this->edit_query( $request->query );
		}

		else if ( $request instanceof Flexical_Admin_Confirm_Delete_Query_Request )
		{
			$response = $this->confirm_delete_query( $request->query );
		}

		else if ( $request instanceof Flexical_Admin_Delete_All_Queries_Request )
		{
			$response = $this->delete_all_queries( $request->confirmed );
		}

		else
		{
			throw new Exception( "Unsupported query request: " . get_class( $request ) );
		}

		return $response;
	}

	private function show_queries()
	{
		return new Flexical_Admin_Show_Queries_Response( $this->get_dao()->read_queries() );
	}

	private function create_query( $query = null )
	{
		return new Flexical_Admin_Create_Query_Response( $this->get_dao()->create_query( $query ),
			$this->get_dao()->read_queries() );
	}

	private function update_query( $query = null )
	{
		$this->get_dao()->update_query( $query );

		return new Flexical_Admin_Update_Query_Response( $query, $this->get_dao()->read_queries() );
	}

	private function delete_query( $query = null )
	{
		$this->get_dao()->delete_query( $query->id );

		return new Flexical_Admin_Delete_Query_Response( $query, $this->get_dao()->read_queries() );
	}

	private function get_listings_using_query( $query_id )
	{
		$listings = array();

		foreach ( $this->get_dao()->read_feed_queries() as $fq_id => $feed_query )
		{
			if ( $feed_query->query_id == $query_id )
			{
				$listings[ $feed_query->listing_id ] = $this->get_dao()->read_listing( $feed_query->listing_id );
			}
		}

		return $listings;
	}

	private function get_listings_using_queries()
	{
		$listings = array();

		foreach ( $this->get_dao()->read_queries() as $query_id => $query )
		{
			foreach ( $this->get_listings_using_query( $query_id ) as $listing_id => $listing )
			{
				$listings[ $listing_id ] = $listing;
			}
		}

		return $listings;
	}

	private function edit_query( $query = null )
	{
		return new Flexical_Admin_Edit_Query_Response( $this->get_dao()->read_query( $query->id ) );
	}

	private function confirm_delete_query( $query = null )
	{
		$listings_using_query = $this->get_listings_using_query( $query->id );

		if ( sizeof( $listings_using_query ) > 0 )
		{
			return new Flexical_Admin_Cant_Delete_Query_In_Use_Response( $this->get_dao()->read_query( $query->id ),
				$listings_using_query, $this->get_dao()->read_queries() );
		}

		else
		{
			return new Flexical_Admin_Confirm_Delete_Query_Response( $this->get_dao()->read_query( $query->id ) );
		}
	}

	private function delete_all_queries( $confirmed = false )
	{
		if ( sizeof( $this->get_listings_using_queries() ) > 0 )
		{
			return new Flexical_Admin_Cant_Delete_Queries_In_Use_Response( $this->get_dao()->read_queries() );
		}

		else if ( $confirmed )
		{
			foreach ( $this->get_dao()->read_queries() as $id => $query )
			{
				$this->get_dao()->delete_query( $id );
			}
		}

		return new Flexical_Admin_Delete_All_Queries_Response( $confirmed, $this->get_dao()->read_queries() );
	}
}

?>
